package com.ww.idiom.source;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.ww.core.activity.MyActivity;
import com.ww.core.util.FileUtil;
import com.ww.core.util.Logger;
import com.ww.idiom.R;
import com.ww.idiom.db.Constants;
import com.ww.idiom.db.IdiomDbHelper;

public class SourceActivity extends MyActivity implements OnClickListener{
int index;
int size;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_source);
		setTitle("数据库管理");
		Button test1 = (Button) findViewById(R.id.test1);
		Button test2 = (Button) findViewById(R.id.test2);
		Button test3 = (Button) findViewById(R.id.test3);
		Button test4 = (Button) findViewById(R.id.test4);
		Button test5 = (Button) findViewById(R.id.test5);
		test1.setOnClickListener(this);
		test2.setOnClickListener(this);
		test3.setOnClickListener(this);
		test4.setOnClickListener(this);
		test5.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.test1:
			try {
				File f =new File(Constants.DATABASE_PATH + Constants.DATABASE_NAME);
				if(f.exists()){
					f.delete();
					Logger.info("f.delete()");
				}
				showMessage("成功创建数据库");
			} catch (Exception e) {
				e.printStackTrace();
				showMessage("创建数据库失败");
			}
			break;
		case R.id.test2:
			showProgressDialog(SourceActivity.this);
			new Thread() {
				public void run() {
					init();
					handler.sendEmptyMessage(1);
				}
			}.start();
			break;
		case R.id.test3:
			showProgressDialog(SourceActivity.this);
			new Thread() {
				public void run() {
					IdiomDbHelper w = new IdiomDbHelper(SourceActivity.this);
					ArrayList<String> list=w.getEmptyWordList();
					size=list.size();
					Logger.info("完善数据库:"+list.size());
					for(int i=0;i<list.size();i++){
						index=i+1;
						IdiomManager.getWordMore(SourceActivity.this, list.get(i));
						handler.sendEmptyMessage(5);
					}
					handler.sendEmptyMessage(2);
				}
			}.start();
			
			break;
		case R.id.test4:
			showProgressDialog(SourceActivity.this);
			new Thread() {
				public void run() {
					IdiomDbHelper w = new IdiomDbHelper(SourceActivity.this);
					w.deleteEmpty();
					handler.sendEmptyMessage(4);
				}
			}.start();
			break;
		case R.id.test5:
			showProgressDialog(SourceActivity.this);
			new Thread() {
				public void run() {
					IdiomDbHelper idiomDbHelper = new IdiomDbHelper(SourceActivity.this);
					List<String> list = idiomDbHelper.getStoreList();
					Logger.info("脏数据:"+list.size());
					for(int i=0;i<list.size();i++){
						new IdiomDbHelper(SourceActivity.this).delete(list.get(i));	
					}
					handler.sendEmptyMessage(4);
				}
			}.start();
			break;
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			switch (msg.what) {
			case 1:
				showMessage("成功创建表");
				break;
			case 2:
				showMessage("成功完善数据库");
				break;
			case 3:
				showMessage("成功完善数据库");
				break;
			case 4:
				showMessage("成功收藏脏数据");
				break;
			case 5:
				showProgressDialog(SourceActivity.this, index+"/"+size);
				break;
			default:
				break;
			}
		};
	};
	
	
	private void init(){
		InputStream inputStream = getResources().openRawResource(R.raw.idions);
		String str=FileUtil.getString(inputStream);
		String[] sss=str.split("、");
		List<String> words = new ArrayList<String>();
		for(int i=0;i<sss.length;i++){
			words.add(sss[i].trim());
		}
		
		IdiomDbHelper h = new IdiomDbHelper(this);
		h.saveWord(words);
	}
}
